
/**
 * 仅微信浏览器可以访问
 * @param {*} req 
 * @param {*} res 
 * @param {*} next 
 */
export function limitAccessBeyondMicroMessenger(req, res, next) {
    if (req.url.indexOf('favicon.ico') >= 0) {
        res.end('NULL');
        return;
    }
    const ua = req.get('User-Agent');
    if (ua && ua.indexOf('MicroMessenger') > 0
        || (req.method === 'GET' && (req.url.indexOf('uploads')
            || req.url.indexOf('.wav') >= 0
            || req.url.indexOf('.jpg') >= 0
            || req.url.indexOf('.mp3') >= 0
            || req.url.indexOf('.txt') >= 0))
    ) {
        next();
    } else {
        res.end('NULL');
    }
}

/**
 * 验证Token
 */
export function checkApiAndToken(req, res, next) {
    if (req.method === 'GET') {
        next();
        return;
    }
    // 不对对微信访问,设备调用保存百度语音接口拦截
    if (req.path.indexOf('wx') > 0
        || req.path.indexOf('baidu_voice_info') >= 0
        || req.path.indexOf('login') > 0
        || req.path.indexOf('ai_api') > 0) {
        next();
        return;
    }

    const accessToken = req.query.access_token;
    if (accessToken === '82500370-c0df-11e5-b57f-57604e1441bf'
        || accessToken === '82500370-c0df-11e5-b57f-57604e1441bk') {
        next();
        return;
    }
    res.apiError(4002, 'Invalid access token');
}


